#ifndef NETWORK_SERVER_H__
#define NETWORK_SERVER_H__

#include "network_libevent.h"
#include "network_stream.hpp"
#include <list>

namespace engine
{
	class NetworkServerListener
	{
	public:
		virtual void onConnected(void* net) = 0;
		virtual void onDisconnected(void* net) = 0;
		virtual void onMessage(void *net, NetworkStream& stream) = 0;
	};

	class NetworkServer
	{
	public:
		bool start(const char* host, uint16 port, uint32 maxConnections);
		void stop();
		
		bool isRunning() const;

		void disconnect(void* net);

		virtual NetworkStream& beginStream();
		virtual void sendStream(const NetworkStream& stream, void* net);

		uint32 getClientIPV4(void* net) const;
		uint16 getClientPort(void* net) const;

		void bindData(void* net, void* data);
		void* bindData(void* net);

		void addListener(NetworkServerListener* listener);
		void removeListener(NetworkServerListener* listener);

		void handleMessages();

	private:
		LibEvent mLibEvent;
		NetworkStream mSendStream;
		NetworkStream mRecvStream;

		typedef std::list<NetworkServerListener*> ListenerList;
		ListenerList mListeners;
	};
}


#endif
